<?php
defined('SYS_IN') OR die('error');
class M_test extends model{
	public static function a(){
		echo "M_test:a()";
	}

	/**
	 * 调用db类测试
	 **/
	public function  dodb(){
		return $this->db->query('show create table `user`');
	}
	
	/**
	 * lastid
	 */
	public function lastid(){
		
		return $this->db->getLastId();
	}
	
	/**
	 * insert
	 */
	public function insert(){
		return $this->db->query('insert into user (name) values ("西木")');
	}
	
	/**
	 * insert_one
	 */
	public function insert_one()
	{
		$this->db->table('user')->insert(array('name'=>'西木_one','sex'=>1));
	}
	
	/**
	 * insert_more 
	 */
	public function insert_more()
	{
		$this->db->table('user')->insert(array('name','sex'),array(array('西木_more_1',1),array('西木_more_2',2)));
	}
	
	/**
	 * select
	 */
	public function select(){
		$db = $this->db->table('user')->where('user.id > :start AND user.id < :end',array(':start'=>100,':end'=>200))->setColumn('user.name')->limit(10,5);
		//$rs2 = $this->db->table('user')->where('id > :start AND id < :end',array(':start'=>150,':end'=>210))->setColumn('name')->limit(10,2)->getAll();
		$rs = $db->leftJoin('user_sub','user.id = user_sub.id')->getAll();
		echo $db->getLastSql();
//		$this->debug->export($rs2);
		$this->debug->export($rs);
		return $rs;
	}
	
	/*
	 * update
	 */
	public function update()
	{
		$db = $this->db->table('user')->where('id < ?',array(100));
		$db->update(array('name'=>'西木_update_11'));
		if($db->isSuccess)
		{
			echo "ok";
		}
	}
	
	public function del()
	{
		$model = $this->db->table('user')->where('id < :test',array(':test'=>100));
		echo $model->del();
		echo $model->getLastSql();
	}
	
	public function getcount()
	{
		$db  = $this->db->table('user')->where('id > ?',array(200));
		echo $db->getCount();
		echo $db->getLastSql();
	}
	
	
	public function transcation()
	{
		$db = $this->db->table('user');
		$db->begin();
		$row = $db->where('id > ?',array(200))->update(array('sex'=>1));
		echo $row;
		if($row<100)
		{
			$db->rollBack();
		}else{
			$db->commit();
		}
	}
	
	public function bak(){
		$bak = new T_mysqlbak($this->db, 'test', 'user');
		$bak->flowBak();
	}
}
?>